package com.beiding.workordersystem.orders.common.service.impl;


import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.utils.DateCalculator;
import com.beiding.workordersystem.common.utils.QueryUtils;
import com.beiding.workordersystem.orders.common.dao.CommissionDao;
import com.beiding.workordersystem.orders.common.entity.Commission;
import com.beiding.workordersystem.orders.common.model.CommissionModel;
import com.beiding.workordersystem.orders.common.model.Moon;
import com.beiding.workordersystem.orders.common.model.Reward;
import com.beiding.workordersystem.orders.common.service.CommissionService;
import com.beiding.workordersystem.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CommissionServiceImpl extends BaseService implements CommissionService {

    @Autowired
    private CommissionDao dao;

    @Autowired
    private UserService userService;

    //年月
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");

    private SimpleDateFormat dateFormatShow = new SimpleDateFormat("yyyy年MM月");

    /*
        时间确定规则
        如果没有开始和结束时间都没有就使用当前时间往前推一年作为时间
        如果开始时间和结束时间有一个,那就使用有的那个推一年作为时间
        如果都有就必须给出一个有效的时间段
     */
    @Override
    public CommissionModel list(Date start, Date to) {

        //必须给出一个有效的区间
        if (start != null && to != null) {
            E.isTrue(to.getTime() >= start.getTime(), "无效时间区间");
        } else if (start == null && to == null) {
            to = new Date();
            start = DateCalculator.addYear(to, -1);
        } else {
            if (start == null) {
                start = DateCalculator.addYear(to, -1);
            } else {
                to = DateCalculator.addYear(start, 1);
            }
        }

        CommissionModel commissionModel = new CommissionModel();

        Map<String, Map<String, List<Commission>>> map = new HashMap<>();

        List<String> head = new ArrayList<>();

        //从开始时间
        Date t = start;
        while (t.getTime() <= to.getTime()) {

            //格式化时间
            String format = dateFormat.format(t);
            //使用like的方式找到对应月份的提成
            List<Commission> commissions = dao.selectList(QueryUtils.getQuery(Commission.class).likeRight(Commission::getCompleteTime, format));

            String showDate = dateFormatShow.format(t);
            head.add(showDate);
            for (Commission commission : commissions) {
                //放入到user的map中
                Map<String, List<Commission>> user = map.computeIfAbsent(commission.getUserId(), k -> new HashMap<>());
                List<Commission> commissionList = user.computeIfAbsent(showDate, k -> new ArrayList<>());
                commissionList.add(commission);
            }

            //增加一个月份
            t = DateCalculator.addMoon(t, 1);
        }

        List<Reward> rewards = new ArrayList<>();

        //整合所有的记录
        map.forEach((userId,commissions)->{

            Reward reward = new Reward();
            reward.setName(userService.getById(userId).getName());
            Map<String, Moon> moonMap = new HashMap<>();
            commissions.forEach((moon,list)->{
                Moon m = new Moon();
                m.setCommissions(list);

                Double sum = 0D;
                for (Commission commission : list) {
                    sum += commission.getValue();
                }
                m.setSum(sum);
                moonMap.put(moon, m);
            });

            reward.setMoons(moonMap);
            rewards.add(reward);
        });

        commissionModel.setRewards(rewards);
        commissionModel.setHead(head);
        return commissionModel;
    }

    @Override
    public CommissionModel satrapList(Date start, Date to) {
        //必须给出一个有效的区间
        if (start != null && to != null) {
            E.isTrue(to.getTime() >= start.getTime(), "无效时间区间");
        } else if (start == null && to == null) {
            to = new Date();
            start = DateCalculator.addYear(to, -1);
        } else {
            if (start == null) {
                start = DateCalculator.addYear(to, -1);
            } else {
                to = DateCalculator.addYear(start, 1);
            }
        }

        CommissionModel commissionModel = new CommissionModel();

        Map<String, Map<String, List<Commission>>> map = new HashMap<>();

        List<String> head = new ArrayList<>();

        //从开始时间
        Date t = start;
        while (t.getTime() <= to.getTime()) {

            //格式化时间
            String format = dateFormat.format(t);
            //使用like的方式找到对应月份的提成
            List<Commission> commissions = dao.selectList(QueryUtils.getQuery(Commission.class).likeRight(Commission::getCompleteTime, format));

            String showDate = dateFormatShow.format(t);
            head.add(showDate);
            for (Commission commission : commissions) {
                //放入到user的map中
                Map<String, List<Commission>> user = map.computeIfAbsent(commission.getSatrapId(), k -> new HashMap<>());
                List<Commission> commissionList = user.computeIfAbsent(showDate, k -> new ArrayList<>());
                commissionList.add(commission);
            }

            //增加一个月份
            t = DateCalculator.addMoon(t, 1);
        }

        List<Reward> rewards = new ArrayList<>();

        //整合所有的记录
        map.forEach((userId,commissions)->{

            Reward reward = new Reward();
            reward.setName(userService.getById(userId).getName());
            Map<String, Moon> moonMap = new HashMap<>();
            commissions.forEach((moon,list)->{
                Moon m = new Moon();
                m.setCommissions(list);

                Double sum = 0D;
                for (Commission commission : list) {
                    sum += commission.getSatrapValue();
                }
                m.setSum(sum);
                moonMap.put(moon, m);
            });

            reward.setMoons(moonMap);
            rewards.add(reward);
        });

        commissionModel.setRewards(rewards);
        commissionModel.setHead(head);
        return commissionModel;
    }

    @Override
    public void save(Commission commission) {
        fillId(commission);
        dao.insert(commission);
    }


}
